package TUILayer;

import java.util.Scanner;
import ControlLayer.DVDCtr;
/**
 * This class is part of an address book system. It's the DVD part of the system UI. This menu is associated with the MainMenuUI class. 
 *
 * The purpose of this class is to communicate with the user and send the requests to the controllayer. 
 * 
 * @author Anders A.P, Anna K., Danny M.P., Jakob K., Mie P.  
 * @version 1.0
 */
public class DvdUI
{
    private DVDCtr dvdCtr;

    /**
     * Constructs an instance of DvdUI, taking no parameters.
     */
    public DvdUI()
    {
        dvdCtr = new DVDCtr();
    }

    /**
     * Initiates the DvDUI class.
     */
    public void start(DVDCtr ctr)
    {
        dvdCtr = ctr;
        dvdMenu();
    }

    /**
     * Starts the UI system in the dvd menu, allowing the user to move through the parts of the menu.
     */   
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit) {
            int choice = writeDVDMenu();
            switch(choice) {
                case 1:
                    startFindDVD();
                    break;
                case 2:
                    startCreateDVD();
                    break;
                case 3:
                    startCreateCopy();
                    break;
                case 4:
                    try {
                        startDeleteDVD();
                    }
                    catch(Exception e) {
                        System.out.println(e);
                        waitInput();
                    }
                    break;
                case 5:
                    startUpdateDVD();
                    break;
                case 6:
                    startUpdateCopy();
                    break;
                case 7:
                    startListAllDvds();
                    break;
                case 8:
                    exit = true;
                    break;
                default:
                    System.out.println("Command not known");
                    break;
            }

        }
    }

    /**
     * Initiates the text in the DvdUI, scanning the user inputs and returns the choice to the caller.
     *
     * @return  choice  The users choice of menu.
     */
    private int writeDVDMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** DVD Menu ***");
        System.out.println(" (1) Show DVD information");
        System.out.println(" (2) Create DVD description");
        System.out.println(" (3) Create DVD specimen");
        System.out.println(" (4) Delete DVD");
        System.out.println(" (5) Update DVD");
        System.out.println(" (6) Update Copy");
        System.out.println(" (7) Show current DVD list");
        System.out.println(" (8) Return");
        System.out.print("\n Make your choice: ");

        int choice = inputChoice();
        return choice;
    }

    /**
     * Method for reading the DVD id. Only accepts integer numbers.
     */
    private int inputDVDNo(String message)
    {
        boolean valid = false;
        int no = 0;
        //System.out.println("Type DVD number: ");
        System.out.println(message);
        while(!valid) {
            Scanner keyboard = new Scanner(System.in);
            try {
                no = keyboard.nextInt();
                valid = true;
            }
            catch(Exception e) {
                System.out.println("Input must only be numbers!");
            }
        }
        return no;
    }

    /**
     * Method for reading the choice, for when the user wants to navigate the menu. Only accepts integer numbers.
     */
    private int inputChoice()
    {
        boolean valid = false;
        int no = 0;
        while(!valid) {
            Scanner keyboard = new Scanner(System.in);
            try {
                no = keyboard.nextInt();
                valid = true;
            }
            catch(Exception e) {
                System.out.println("Input must only be numbers!");
            }
        }
        return no;
    }

    /**
     * Method for reading the movie title.
     */
    private String inputTitle(String message)
    {
        Scanner keyboard = new Scanner(System.in);
        //System.out.println("Type DVD title: " );
        System.out.println(message);
        String title = keyboard.nextLine();
        return title;
    }

    /**
     * Method for reading the copy serialnumber as input. Only accepts integer numbers.
     */
    private int inputCopyNo(String message)
    {
        boolean valid = false;
        int no = 0;
        //System.out.println("Type the copy serialnumber: ");
        System.out.println(message);
        while(!valid) {
            Scanner keyboard = new Scanner(System.in);
            try {
                no = keyboard.nextInt();
                valid = true;
            }
            catch(Exception e) {
                System.out.println("Input must only be numbers!");
            }
        }
        return no;
    }

    /**
     * Method for reading a artist as input. 
     *
     * @return  s   The user input as a String.
     */
    private String inputArtist(String message)
    {
        Scanner keyboard = new Scanner(System.in);
        //System.out.println("Type the artist of the movie: ");
        System.out.println(message);
        //String s = keyboard.next(); //DEMO keyboard.next() only reads the first word of what the user typed in. 
        String s = keyboard.nextLine();
        return s;
    }

    /**
     * Method for recording the publicationdate.
     * 
     * @return  s   The publication date as a String.
     */
    private String inputPubDate(String message)
    {
        Scanner keyboard = new Scanner(System.in);
        //System.out.println("Type the publicationdate of the movie: ");
        System.out.println(message);
        String s = keyboard.next();
        return s;
    }

    /**
     * Method for recording the purchasedate.
     *
     * @return  s   The purchase date as a String.
     */
    private String inputPurchaseDate(String message)
    {
        Scanner keyboard = new Scanner(System.in);
        //System.out.println("Type the purchasedate of the copy: ");
        System.out.println(message);
        String s = keyboard.next();
        return s;
    }

    /**
     * Method for recording the purchaseprice. Only accepts double numbers. 
     *
     * @return  no  The purchase price as a floating point number.
     */
    private double inputPurchasePrice(String message)
    {
        boolean valid = false;
        double no = 0;
        //System.out.println("Type the purchase price of the copy: ");
        System.out.println(message);
        while(!valid) {
            Scanner keyboard = new Scanner(System.in);
            try {
                no = keyboard.nextDouble();
                valid = true;
            }
            catch(Exception e) {
                System.out.println("Input must only be numbers!");
            }
        }
        return no;
    }

    /**
     * Method to create a sub-menu to choose wether to search with an id or a title.
     * If the userinput is not either 1 or 2, the user will be returned to the main menu in the DVDUI.
     *
     * @return  found   Returns true or false, depending on wether or not the DVD object has been found.
     */
    private boolean startFindDVD()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" (1) Find movie by ID.");
        System.out.println(" (2) Find movie by title.");
        boolean found = false;
        int choice = inputChoice();
        switch(choice) {
            case 1:
                found = startFindDVDID();
                waitInput();
                return found;
            case 2:
                found = startFindDVDTitle();
                waitInput();
                return found;
            default:
                System.out.println("Command not known, returning to DVD main menu.");
                waitInput();
                return found;
        }
    }

    /**
     * Method for finding a DVD using the DVDs id as input.
     *
     * @return  true    If DVD is found.
     * @return  false   If DVD is not found.
     */
    private boolean startFindDVDID()
    {   
        int no = inputDVDNo("Number of DVD to be found: ");
        try {
            System.out.println(dvdCtr.findDVDID(no));
            return true;
        }
        catch(Exception e) {
            System.out.println("DVD not found");
            return false;
        }
    }

    /**
     * Method for finding a DVD using the DVDs title as input. 
     *
     * @return  true    If DVD is found.
     * @return  false   If DVD is not found.
     */
    private boolean startFindDVDTitle()
    {
        try {
            System.out.println(dvdCtr.findDVDTitle(inputTitle("Title of DVD to be found: ")));
            return true;
        }
        catch(Exception e) {
            System.out.println("DVD not found");
            return false;
        }
    }

    /**
     * Method for creating a DVD.
     */
    private void startCreateDVD()
    {
        String title = inputTitle("Title: ");
        String artist = inputArtist("Artist: ");
        String pubDate = inputPubDate("Publication date: ");
        dvdCtr.createDVD(title, artist, pubDate);
    }

    /**
     * Method for creating a copy. Requires the id of a DVD for the copy to be assigned to.
     */
    private void startCreateCopy()
    {
        int     dvdID = inputDVDNo("Number of DVD the copy is assigned to: " );
        int     serialNo = inputCopyNo("Serial number of copy: ");
        String  purchaseDate = inputPurchaseDate("Purchase date: ");
        double  purchasePrice = inputPurchasePrice("Purchase price: ");
        try {
            dvdCtr.createCopy(dvdID, serialNo, purchaseDate, purchasePrice);
        }
        catch(Exception e) {
            System.out.println("DVD not found");
            waitInput();
        }

    }

    /**
     * Method for deleting a DVD.
     */
    private void startDeleteDVD()
    {
        int id = inputDVDNo("Number of DVD to be deleted: ");
        dvdCtr.deleteDVD(id);

    }

    /**
     * Method for printing all the DVDs and the copies assigned to them. 
     */
    private void startListAllDvds()
    {
        System.out.print(dvdCtr.listAllDvds());
        waitInput();
    }

    /**
     * Method for updating DVD. (Old version: Uses startDeleteDVD and startCreateDVD.)
     */
    private void startUpdateDVD()
    {
        //DEMO
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Title of DVD to be updated: ");
        String oldTitle = keyboard.nextLine();
        try {
            //startDeleteDVD();
            //startCreateDVD();
            dvdCtr.updateDVD(oldTitle, inputTitle("New title: "), inputArtist("New artist: "), inputPubDate("New publication date: "));
        }
        catch(Exception e) {
            System.out.println(e);
            waitInput();
        }
    }

    /**
     * Method for updating Copy.
     */
    private void startUpdateCopy()
    {
        if(startFindDVD()) {
            try {
                dvdCtr.updateCopy(inputDVDNo("DVD number: "), inputCopyNo("New serial number: "), inputPurchaseDate("New purchase date: "), 
                inputPurchasePrice("New purchase price: "));
            }
            catch(Exception e) {
                System.out.println(e);
                waitInput();
            }
        }
    }

    /**
     * Method for waiting until the user types again. Used after we've printed something, and yet want to return to the main menu.
     */
    private void waitInput()
    {
        System.out.println("Hit enter to continue");
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
}